/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modulo1;

import Modelo.DAL.ConexionBD;
import Modelo.DAL.Edificio_DAO;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONObject;
import org.json.JSONArray;

/**
 *
 * @author glpc
 */
public class listaEdificioAction extends org.apache.struts.action.Action {

  /* forward name="success" path="" */
  private static final String SUCCESS = "success";
  private static final String FAILURE = "failure";

  /**
   * This is the action called from the Struts framework.
   * @param mapping The ActionMapping used to select this instance.
   * @param form The optional ActionForm bean for this request.
   * @param request The HTTP Request we are processing.
   * @param response The HTTP Response we are processing.
   * @throws java.lang.Exception
   * @return
   */
    @Override
 public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {

    String nombre = request.getParameter("nombre");
    String siglas = request.getParameter("siglas");
    String sede = request.getParameter("sede");
    String offset = request.getParameter("offset");
    String limit = request.getParameter("limit");

    //String where = "SELECT *,(Select count(*) from edificio) FROM edificio where 1 = 1 ";
    String where = "select e.nombre_espacio, ed.siglas , a.descripcion, ed.nombre_sede, (Select count(*) from espacio where tipo = 'Edificio') from espacio e, edificio ed, area a where e.id_area=a.id_area and ed.nombre_edificio = e.nombre_espacio and e.tipo = 'Edificio'";

    if (nombre != null && !nombre.equals("")) {
      where += " and nombre_edificio = '" + nombre + "'";
    }

    if (sede != null && !sede.equals("")) {
      where += " and e.nombre_sede = '" + sede + "'";
    }

    where += " ORDER BY e.nombre_espacio asc";
    if (limit != null && !limit.equals("0")) {
      where += " limit " + limit + " offset " + offset;
    }
    System.out.println(where);
    response.setContentType("text/json");
    
    JSONObject ext = new JSONObject();
    Edificio_DAO eDao = new Edificio_DAO();
    ext.put("consultarEdificio", eDao.consultarEdificioTabla(where));
    PrintWriter pw = response.getWriter();
    pw.write(ext.toString());
    pw.flush();
    pw.close();

    return mapping.findForward(SUCCESS);
  }
}


  /* @Override
  public ActionForward execute(ActionMapping mapping, ActionForm form,
          HttpServletRequest request, HttpServletResponse response)
          throws Exception {
    response.setContentType("text/json");
    JSONObject data = new JSONObject();
    data.put("Edificio", obtenerlistaEdificios());

    PrintWriter pw = response.getWriter();
    pw.write(data.toString());
    pw.flush();
    pw.close();

    return mapping.findForward(SUCCESS);
  }

  public JSONObject obtenerlistaEdificios() {
    ConexionBD conex = new ConexionBD();
    JSONObject edificios = new JSONObject();
    try {
      Statement st = conex.session.createStatement();
      ResultSet rs = st.executeQuery("SELECT nombre_edificio FROM edificio ORDER BY nombre_edificio ");
      int i = 0;
      while (rs.next()) {
        JSONObject edificio = new JSONObject();
        edificio.put("edificio", rs.getString(1));
        edificios.put("" + i++, edificio);
      }
    } catch (Exception e) {
    }
    return edificios;
  }
}
*/